var textArray = new Array("仅自己可见","仅好友可见","同网络及好友可见","所有人可见");
var flag = true;
$(document).ready(function(){
	$(".privacy_select").each(function(){
			$(this).html(textArray[$(this).html()]);
			$(this).bind("click",function(){setPrivacySetting(this);});
			$(this).mouseover(function(){  
				$(this).addClass("over");}).mouseout(function(){ 
                 	$(this).removeClass("over");});
		//alert($(this).prev().val());
	});
	
	$(".feedaction").each(function(){
		$(this).bind("click",function(){postFeedSetting(this);});	
	})

});

function setPrivacySetting(obj){
	//$('.privacySetting').click(function(){
	if(flag){
		flag = false;
		$(obj).mouseover(function(){  
				$(obj).removeClass("over");});
		var input = ' <select class="privacySelect">';
		for(var i=0 ; i<textArray.length ; i++){
			input += '<option value="'+i+'" ';
			if(textArray[i] == $(obj).html()){
				input += "selected";
			}
			input += '>'+textArray[i]+'</option>';			
		}
		input += '</select>';
		$(obj).html(input); 		
		$('.privacySelect').focus();
		var sv = $('.privacySelect').val()
		$('.privacySelect').blur(function(){
			setPrivacySelect(this,sv);
		})
	}
	//});
}

function setPrivacySelect(obj,v){
	flag = true;
	var pObj = $(obj).parent();
	if( v != $(obj).val()){
		$.post(APP+"/ajax/saveprivacysetting", {field: $(pObj).attr("tag"), value: $(obj).val() } , 
		function(data){
			if("success"==data){
				msg(pObj,"设置成功");
			}else{
				msg(pObj,"设置失败");
			}
		});
		
	}
	$(pObj).html(textArray[$(obj).val()]);
	$(pObj).bind("click",function(){setPrivacySetting(this);});
	$(pObj).mouseover(function(){  
				$(pObj).addClass("over");}).mouseout(function(){ 
                 	$(pObj).removeClass("over");});
	//alert($(pObj).prev().attr("name"));
}

function postFeedSetting(obj){
	var value = 0;
	var field = $(obj).attr("name")+"action";
	if($(obj).attr("checked")){
		value = 1;
	}
	$.post(APP+"/ajax/saveprivacysetting", {field: field, value: value } );
	msg(obj,"设置成功");
}